package com.utils.imageloader 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	/**
	 * 图片加载器
	 * @author axiom
	 */
	public class ImageLoaderQ extends EventDispatcher implements IEventDispatcher
	{
		private var _imageCache:Array = null;  //二维数组，1为id 2为loader
		private var _number:int = 15;			//同时加载个数
		private var _UrlLoaderArr:Array = null;
		private var _loaderAddress:Array = null;
		private static var _instance:ImageLoaderQ = null;
		
		public function ImageLoaderQ() 
		{
			_imageCache = new Array();
			
			_UrlLoaderArr = new Array();
			
			_loaderAddress = new Array();	
		}
		
		private function clear():void 
		{
			for (var k:int = 0; k < _UrlLoaderArr.length; k++)
			{
				if (arguments[0] == _UrlLoaderArr[k][2])
				{
					var temp:Array = _UrlLoaderArr.splice(k, 1)[0];//加载失败的,清理出去
					_imageCache[_imageCache.length] = new Array();
					_imageCache[_imageCache.length - 1][0] = temp[2];
					_imageCache[_imageCache.length - 1][1] = null;
					var obj:Object = new Object();
					obj['imageArr'] = _imageCache[_imageCache.length-1];
				//	trace("加载时间超过5秒，被清理出去，标志为此图片未找到");
				//	trace("图片:" + arguments[0] + "没找到.");
					this.dispatchEvent(new ImageLoadEvent(ImageLoadEvent.IMAGE_LOADED, obj));
					break;
				}
			}
			loading();
		}
		
		public function loader(str:String,mid:String):void
		{
			for (var i:int = 0; i < _imageCache.length; i++)
			{
				if (_imageCache[i][0] == mid)
				{	
					var obj:Object = new Object();
					obj['imageArr'] = _imageCache[i];
				//	trace("找到缓存图片:mid="+_imageCache[i][0]);
					this.dispatchEvent(new ImageLoadEvent(ImageLoadEvent.IMAGE_LOADED, obj));
					return;
				}
			}
			
			_loaderAddress[_loaderAddress.length] = new Array();
			_loaderAddress[_loaderAddress.length - 1][0] = str;
			_loaderAddress[_loaderAddress.length - 1][1] = mid;
			
			loading();	
		}
		
		private function loading():void 
		{
			
		//	trace("_loaderAddress.length " + _loaderAddress.length );
		//	trace("_UrlLoaderArr.length "+_UrlLoaderArr.length );
			if (_loaderAddress.length == 0) return;
			
			for (var i:int = 0; i < _imageCache.length; i++)
			{
				if (_imageCache[i][0] ==_loaderAddress[0][1])
				{	
					_loaderAddress.shift();
					var obj:Object = new Object();
					obj['imageArr'] = _imageCache[i];
				//	trace("找到缓存图片:mid="+_imageCache[i][0]);
					this.dispatchEvent(new ImageLoadEvent(ImageLoadEvent.IMAGE_LOADED, obj));
					return;
				}
			}
			
			if (_UrlLoaderArr.length < _number)
			{
				var shiftArr:Array = _loaderAddress.shift();
				_UrlLoaderArr[_UrlLoaderArr.length] = new Array();
				_UrlLoaderArr[_UrlLoaderArr.length - 1][0] = shiftArr[0];
				_UrlLoaderArr[_UrlLoaderArr.length - 1][1] = new Loader();
				(_UrlLoaderArr[_UrlLoaderArr.length - 1][1] as Loader).contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
				(_UrlLoaderArr[_UrlLoaderArr.length - 1][1] as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE, imageDataComplete);
				_UrlLoaderArr[_UrlLoaderArr.length - 1][2] = shiftArr[1];
				_UrlLoaderArr[_UrlLoaderArr.length - 1][3] = setTimeout(clear, 4000, _UrlLoaderArr[_UrlLoaderArr.length - 1][2]);
				
				try 
				{
					(_UrlLoaderArr[_UrlLoaderArr.length - 1][1] as Loader).load(new URLRequest(_UrlLoaderArr[_UrlLoaderArr.length - 1][0]));
				}
				catch (e:Error)
				{
					
				}
			}
		}
		
		private function onIoError(e:IOErrorEvent):void 
		{
		//	trace("图片加载失败");
		}
			
		private function imageDataComplete(e:Event):void 
		{
           for (var i:int = 0; i < _UrlLoaderArr.length; i++)
		   {
			   if (_UrlLoaderArr[i][1] == e.currentTarget.loader)
			   {
				clearTimeout(_UrlLoaderArr[i][3]);
				var temp:Array= _UrlLoaderArr.splice(i, 1)[0];
				_imageCache[_imageCache.length] = new Array();
				_imageCache[_imageCache.length - 1][0] = temp[2];
				_imageCache[_imageCache.length - 1][1] = temp[1];
				var obj:Object = new Object();
				obj['imageArr'] = _imageCache[_imageCache.length-1];
			//	trace("下载完毕"+_imageCache[_imageCache.length-1][0]);
				this.dispatchEvent(new ImageLoadEvent(ImageLoadEvent.IMAGE_LOADED, obj));
				loading();
				return;  
			   }
		   }
		}
		
		public static function get instance():ImageLoaderQ
		{
			if (_instance == null)
			{
				_instance = new ImageLoaderQ();
			}
			return _instance;
		}
	}
}
